﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Cosmetic.Logic;

namespace WebApp.Management.Product
{
    public partial class GoodChoice : PageBase2
    {
        ProductManager theProductManager = new ProductManager();
        private RoleManager m_RoleManager = new RoleManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }

            if (!IsPostBack)
            {
                LoadGoodChoiceProduct();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OnSelect")
            {
                //往sale表里插入记录
                //SaleManager.AddProductToSale(e.CommandArgument.ToString());
                theProductManager.InsertGoodChoice(new Guid(e.CommandArgument.ToString()));
                LoadGoodChoiceProduct();
                Page.RegisterStartupScript("openalert","<script>添加成功!</script>");
            }
        }

        private void LoadGoodChoiceProduct()
        {
            this.GVGoodChoiceProducts.DataSource = theProductManager.GetGoodChoiceProducts();
            this.GVGoodChoiceProducts.DataBind();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            SearchProduct(this.TextBox1.Text.Trim());
        }

        private void SearchProduct(string txtSearchContent)
        {
            HiddenField1.Value = txtSearchContent;
            this.GridView1.DataSource = theProductManager.SearchProductInfoByName(txtSearchContent);
            this.GridView1.DataBind();
        }

        protected void GVGoodChoiceProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OnDelete")
            {
                //SaleManager.DeleteSaleProduct(e.CommandArgument.ToString());
                theProductManager.DeleteGoodChoiceProduct(new Guid(e.CommandArgument.ToString()));
                LoadGoodChoiceProduct();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            SearchProduct(HiddenField1.Value);
            //LoadGoodChoiceProduct();
        }

        protected void GVGoodChoiceProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GVGoodChoiceProducts.PageIndex = e.NewPageIndex;
            LoadGoodChoiceProduct();
        }

        protected void GVGoodChoiceProducts_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GVGoodChoiceProducts.EditIndex = e.NewEditIndex;
            LoadGoodChoiceProduct();
        }

        protected void GVGoodChoiceProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            HiddenField hf = this.GVGoodChoiceProducts.Rows[e.RowIndex].FindControl("HiddenField2") as HiddenField;
            TextBox tb = this.GVGoodChoiceProducts.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
            

            theProductManager.UpdateGoodChoiceProducts(hf.Value, int.Parse(tb.Text));
            this.GVGoodChoiceProducts.EditIndex = -1;
            LoadGoodChoiceProduct();
        }

        protected void GVGoodChoiceProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GVGoodChoiceProducts.EditIndex = -1;
            LoadGoodChoiceProduct();
        }
    }
}
